package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前添加的商品是否在购物车已存在
        //查询时只查询当前用户id对应的购物车数据
        //构造查询条件对象shoppingCart  如果是添加套餐，则有userId,setmealId两个查询条件，如果添加菜品，则有userId，dishId，dishFlavor三个查询条件
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart); //拷贝属性
        shoppingCart.setUserId(BaseContext.getCurrentId()); //设置当前用户id

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //如果存在，则将数量+1，执行update修改数据
        if (list != null && list.size() > 0) {
            //虽然返回值是List，但是按照业务逻辑最多只会查出一条数据，再添加会在该条数据数量字段上+1
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber() + 1);

            shoppingCartMapper.updateNumberById(cart);
        }

        //否则添加商品到数据库中，默认数量为1，名称，金额，图片 根据菜品/套餐id查询
        else {
            //判断是菜品还是套餐，去对应数据表中查询相关数据

            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();

            if (dishId != null) {
                //添加菜品到购物车业务

                //查询菜品相关名称，金额，图片数据
                Dish dish = dishMapper.getById(dishId);

                //补充购物车对象数据
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());

            } else {
                //添加套餐到购物车业务

                //查询套餐相关名称，金额，图片数据
                Setmeal setmeal = setmealMapper.getById(setmealId);

                //补充购物车对象数据
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());

            }

            //抽取相同代码
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1); //默认数量为1

            //插入购物车表
            shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     * 查看购物车
     * @return
     */
    public List<ShoppingCart> showShoppingCart() {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //将用户id作为查询参数封装到ShoppingCart对象中
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(userId);
        List<ShoppingCart>  shoppingCartList = shoppingCartMapper.list(shoppingCart);

        return shoppingCartList;
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();
        //删除该用户对应的所有购物车数据
        shoppingCartMapper.deleteByUserId(userId);
    }

    /**
     * 删除购物车中一个商品
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //获取当前用户id
        Long userId = BaseContext.getCurrentId();

        //构造查询条件 userid,   dishid,dishFlavor || setmealid
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(userId);

        //查询当前菜品/套餐数量是否为1，如果为1，删除时将该条数据删除，否则数量-1
        //虽然返回值是List，但是按照业务逻辑最多只会查出一条数据
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.list(shoppingCart);

        if (shoppingCartList != null && shoppingCartList.size() > 0) {
            shoppingCart = shoppingCartList.get(0);
            if (shoppingCart.getNumber() > 1) {
                //将数量-1
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);

                shoppingCartMapper.updateNumberById(shoppingCart);
            }
            else {
                //当数量为1时，删除时将该条数据删除
                shoppingCartMapper.deleteById(shoppingCart.getId());
            }
        }

    }


}
